package com.mutouren.modules.datapush.client;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.mutouren.modules.datapush.LocalManager;
import com.mutouren.modules.datapush.base.Charset;
import com.mutouren.modules.datapush.base.Constant;
import com.mutouren.modules.datapush.base.ContentType;
import com.mutouren.modules.datapush.base.MethodType;
import com.mutouren.modules.datapush.base.Pair;
import com.mutouren.modules.datapush.base.PushClient;

/**
 * Demo: 本地模式，不需要启动 mtr-datapush-server 服务
 * 
 */
@SuppressWarnings("unused")
public class PushClientTest_Local1 {
	
	@BeforeClass
	public static void before() {
		System.out.println("event: before");
		
		//LocalManager.open(); // 推送数据不进行持久化
		LocalManager.open("e:/logs-data/push-data", true); // 推送数据持久化
	}	
	
	@AfterClass
	public static void after() {
		LocalManager.close();
		System.out.println("event: after");		
	}	

	@Test
	public void test1() throws InterruptedException {

		// 以下两种写法等价
		PushClient dpClient = new SimplePushClient("http://localhost:8080/mtr-web-demo/api/demo/hello");
		//PushClient dpClient = new SimplePushClient("http://localhost:8080/mtr-web-demo/api/demo/hello", Constant.defaultMethodAlias);

		String dataPackage = "Hello, I am local server"; 
		dpClient.send(dataPackage);

		Thread.sleep(10000);
		System.out.println("event: test() is end");	
	}
	
	@Test
	public void test2() throws InterruptedException {
		PushClient dpClient = new SimplePushClient("http://localhost:8080/mtr-web-demo/api/demo/delay");

		List<Pair<String, String>> listPair = new ArrayList<Pair<String, String>>();
		listPair.add(new Pair<String, String>("delaytime", "5"));
		listPair.add(new Pair<String, String>("info", "hello"));
		
		dpClient.send(listPair, MethodType.GET, Charset.UTF_8);		

		Thread.sleep(10000);
		System.out.println("event: test() is end");	
	}
		
	@Test
	public void test_more() throws InterruptedException {
		PushClient dpClient = new SimplePushClient("http://localhost:8080/mtr-web-demo/api/demo/hello");
		for(int i = 0; i < 1000; i ++ ) {
			String dataPackage = String.format("{\"code\":%d,\"message\":\"how are you\"}", i); 
			dpClient.send(dataPackage, ContentType.JSON, Charset.UTF_8);
		}

		Thread.sleep(30000);
		System.out.println("event: test() is end");	
	}
	
	
	
}
